<?php
elgg_push_context('widgets');
global $CONFIG, $vars;
$config = elgg_get_plugin_from_id('igolf_news');
function catch_that_image($content) {
	$first_img = '';
	ob_start();
	ob_end_clean();
	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
	$first_img = $matches [1][0];
	if(empty($first_img)){
		$first_img = "images/noimages.jpg";
	}
	return $first_img;
}
$conn = mysqli_connect($config->mysqlhostname,$config->mysqluser,$config->mysqlpass,$config->mysqldbname,$config->mysqlport);
$sql ="SELECT post_date,post_content,post_title,post_status,post_type,guid FROM wp_posts WHERE post_status='publish' AND post_type='post' LIMIT 10";
$query = mysqli_query($conn,$sql) or die(mysqli_error());
echo "<div class='elgg-module elgg-module-highlight elgg-module-featured'>";
echo "<div class='elgg-head'><h3>Latest News</h3></div>";
echo "<div class='elgg-body'>";
echo "<ul class='igolf-news'>";
while($row1 = mysqli_fetch_array($query)){
	echo "<li>";?>
	<img src="<?php echo catch_that_image($row1['post_content']); ?>" />
	<div class="igolf_news_intro">
		<a href="<?php echo $row1['guid']?>" target="_blank"><h3><?php echo $row1['post_title']?></h3></a>
		<div class="igolf_news_content">
		<?php 
			$content = $row1['post_content'];
			$content = preg_replace("/<img[^>]+\>/i", "", $content);
		echo substr($content,0,130)."...";?>
			
			<span class="post_date"><?php echo $row1['post_date']?></span>
		</div>
	</div>
	<?php echo "</li>";
}
echo "</ul>";
echo "</div>";
echo "</div>";
mysqli_close($conn);
?>